package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyInventory;
import cn.stylefeng.guns.sys.modular.system.entity.DsstoreBill;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillParam;
import cn.stylefeng.guns.sys.modular.system.model.result.DsstoreBillResult;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 控制器
 *
 * @author jiangge
 * @Date 2020-09-15 20:24:02
 */
@Controller
@RequestMapping("/dsstoreBill")
public class DsstoreBillController extends BaseController {

    private String PREFIX = "/modular/system/dsstoreBill";

    @Autowired
    private DsstoreBillService dsstoreBillService;

    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/dsstoreBill.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/dsstoreBill_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/dsstoreBill_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DsstoreBillParam dsstoreBillParam) {
        this.dsstoreBillService.add(dsstoreBillParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DsstoreBillParam dsstoreBillParam) {
        this.dsstoreBillService.update(dsstoreBillParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DsstoreBillParam dsstoreBillParam) {
        this.dsstoreBillService.delete(dsstoreBillParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DsstoreBillParam dsstoreBillParam) {
        DsstoreBill detail = this.dsstoreBillService.getById(dsstoreBillParam.getStoreBillDtid());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(String timeLimit, DsstoreBillParam ruParam) {
        String startTime;
        String endTime;
        //日期不为空
        if (StringUtils.isNotBlank(timeLimit)) {
            //当月第一天
            startTime = timeLimit.substring(0, 10);
            endTime = timeLimit.replaceAll(" ", "").substring(11, 21);
        } else {
            SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");
            String time = sformat.format(new Date());
            StringBuilder sb = new StringBuilder(time);
            sb.replace(time.length() - 2, time.length(), "01");
            startTime = sb.toString();
            endTime = time;

        }
        //查询
        DsstoreBillParam param = new DsstoreBillParam();
        param.setShopCode(ruParam.getShopCode());
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        param.setItemTypeCode("101");
        param.setItemCode(ruParam.getItemCode());
        param.setBillType(ruParam.getBillType());

        return this.dsstoreBillService.findPageBySpec(param);
    }


    /**
     * 获取库存信息
     *
     */
    @ResponseBody
    @RequestMapping(value = "/getStoreData")
    public LayuiPageInfo getStoreData(String timeLimit, DsstoreBillParam ruParam) {
        String startTime;
        String endTime;
        //日期不为空
        if (StringUtils.isNotBlank(timeLimit)) {
            //当月第一天
            startTime = timeLimit.substring(0, 8) + "01";
            endTime = timeLimit.replaceAll(" ", "").substring(11, 21);
        } else {
            SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");
            String time = sformat.format(new Date());
            StringBuilder sb = new StringBuilder(time);
            sb.replace(time.length() - 2, time.length(), "01");
            startTime = sb.toString();
            endTime = time;

        }
        //查询
        DsstoreBillParam param = new DsstoreBillParam();
        param.setShopCode(ruParam.getShopCode());
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        param.setItemTypeCode("101");
        param.setItemCode(ruParam.getItemCode());
        return dsstoreBillService.getConutDataResult(param);
    }

    @RequestMapping(value = "/selectMoney")
    @ResponseBody
    private Object selectMoney(BtyInventory params) {
        //目前只有酱料
        params.setItemTypeCode("101");
        //构建查询参数
        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");
        String time = sformat.format(new Date());
        StringBuilder sb = new StringBuilder(time);
        sb.replace(time.length() - 2, time.length(), "01");
        String startTime = sb.toString();
        String endTime = time;
        params.setStartTime(startTime);
        params.setEndTime(endTime);
        DsstoreBillResult dsstoreBill = dsstoreBillService.selectRkAndTotalRkje(params);
        BigDecimal unitPrice = dsstoreBill.getUnitPrice();

        //输入数量*单价
        BigDecimal result = new BigDecimal(params.getSl()).multiply(unitPrice);
        Boolean flag = false;
        //大于等于输入的金额
        if (result.compareTo(params.getWarnPrice()) == 1) {
            flag = true;
        }
        return flag;
    }

    /**
     * 处理不合理数据，0E-8，0e-8
     */
    private String convertNum(String data) {
        if ("0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return "0";
        } else {
            return data;
        }
    }

    /**
     * 门店收发明细页面
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("shouFaMingXiIndex")
    public String shouFaMingXiIndex() {
        return PREFIX + "/shouFaMingXi.html";
    }


}


